<html>
    <head>
        <title>Itowns - Planar example</title>

        <script type="importmap">
        {
            "imports": {
                "itowns": "../dist/itowns.js",
                "debug": "../dist/debug.js",
                "dat": "https://unpkg.com/dat.gui@0.7.9/build/dat.gui.module.js",
                "GuiTools": "./jsm/GUI/GuiTools.js",
                "LoadingScreen": "./jsm/GUI/LoadingScreen.js",
                "itowns_widgets": "../dist/itowns_widgets.js",
                "three": "https://unpkg.com/three@0.170.0/build/three.module.js",
                "three/addons/": "https://unpkg.com/three@0.170.0/examples/jsm/"
            }
        }
        </script>

        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/example.css">
        <link rel="stylesheet" type="text/css" href="css/LoadingScreen.css">
        <link rel="stylesheet" type="text/css" href="css/widgets.css">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div id="description">
            Key bindings
            <ul>
              <li>Left-Click: camera translation (drag)</li>
              <li>Right-Click: camera translation (pan)</li>
              <li>Ctrl + Left-Click: camera rotation (orbit)</li>
              <li>Spacebar / Wheel-Click: smart travel</li>
              <li>Mouse Wheel: zoom in/out</li>
              <li>T: orient camera to a top view</li>
              <li>Y: move camera to start position</li>
            </ul>
        </div>
        <div id="viewerDiv" class="viewer"></div>

        <script type="module">

            import GuiTools from 'GuiTools';
            import setupLoadingScreen from 'LoadingScreen';
            import * as THREE from 'three';
            import * as itowns from 'itowns';
            import * as debug from 'debug';
            import * as itowns_widgets from 'itowns_widgets';
            // Define crs projection that we will use (taken from https://epsg.io/3946, Proj4js section)
            itowns.CRS.defs('EPSG:3946', '+proj=lcc +lat_1=45.25 +lat_2=46.75 +lat_0=46 +lon_0=3 +x_0=1700000 +y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
            /* global itowns, setupLoadingScreen, GuiTools, debug */
            // # Planar (EPSG:3946) viewer

            var extent;
            var viewerDiv;
            var view;
            var p;
            var menuGlobe;
            var d;

            // Define geographic extent: CRS, min/max X, min/max Y
            extent = new itowns.Extent(
                'EPSG:3946',
                1837816.94334, 1847692.32501,
                5170036.4587, 5178412.82698);

            // `viewerDiv` will contain iTowns' rendering area (`<canvas>`)
            viewerDiv = document.getElementById('viewerDiv');

            // Instanciate PlanarView*
            view = new itowns.PlanarView(viewerDiv, extent, { placement: { heading: 49.6, range: 6200, tilt: 17 } });
            setupLoadingScreen(viewerDiv, view);

            // Add a WMS imagery source
            var wmsImagerySource = new itowns.WMSSource({
                extent: extent,
                name: 'ortho_latest',
                url: 'https://imagerie.data.grandlyon.com/wms/grandlyon',
                version: '1.3.0',
                crs: 'EPSG:3946',
                format: 'image/jpeg',
            });

            // Add a WMS imagery layer
            var wmsImageryLayer = new itowns.ColorLayer('wms_imagery', {
                updateStrategy: {
                    type: itowns.STRATEGY_DICHOTOMY,
                    options: {},
                },
                source: wmsImagerySource,
            });

            view.addLayer(wmsImageryLayer);

            // Add a WMS elevation source
            var wmsElevationSource = new itowns.WMSSource({
                extent: extent,
                url: 'https://imagerie.data.grandlyon.com/wms/grandlyon',
                name: 'MNT2018_Altitude_2m',
                crs: 'EPSG:3946',
                width: 256,
                format: 'image/jpeg',
            });

            // Add a WMS elevation layer
            var wmsElevationLayer = new itowns.ElevationLayer('wms_elevation', {
                useColorTextureElevation: true,
                colorTextureElevationMinZ: 144,
                colorTextureElevationMaxZ: 622,
                source: wmsElevationSource,
            });

            view.addLayer(wmsElevationLayer);

            var wfsCartoSource = new itowns.WFSSource({
                url: 'https://data.geopf.fr/wfs/ows?',
                version: '2.0.0',
                typeName: 'BDCARTO_V5:zone_d_habitation',
                crs: 'EPSG:3946',
                ipr: 'IGN',
                format: 'application/json',
            });

            var wfsCartoStyle = {
                zoom: { min: 0, max: 20 },
                text: {
                    field: '{toponyme}',
                    color: 'white',
                    transform: 'uppercase',
                    size: 15,
                    haloColor: 'rgba(20,20,20, 0.8)',
                    haloWidth: 3,
                },
            };

            var wfsCartoLayer = new itowns.LabelLayer('wfsCarto', {
                source: wfsCartoSource,
                style: wfsCartoStyle,
            });

            view.addLayer(wfsCartoLayer);

            // Add a scale :
            const scale = new itowns_widgets.Scale(view, {
                position: 'bottom-right',
                translate: { x: -70 },
            });

            // Request redraw
            view.notifyChange();

            if (view.isDebugMode) {
                menuGlobe = new GuiTools('menuDiv', view);
                menuGlobe.addImageryLayersGUI(view.getLayers(function gui(l) { return l.isColorLayer; }));
                menuGlobe.addElevationLayerGUI(wmsElevationLayer);
                debug.createTileDebugUI(menuGlobe.gui, view);
            }
            window.view = view;
            window.itowns = itowns;
            window.THREE = THREE;
            window.extent = extent;

        </script>
    </body>
</html>
